package com.dzu.easy;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ZhaoDong
 * @date 2022/9/30 11:18
 * @description 387. 字符串中的第一个唯一字符
 */
public class Test387 {
    public static void main(String[] args) {
        System.out.println(firstUniqChar("leetcode"));
    }


    public static int firstUniqChar(String s) {
        Map<Character, Integer> map = new HashMap<>();

        for (int i = 0; i < s.length(); i++) {
            char key = s.charAt(i);
            map.put(key, map.getOrDefault(key, 0) + 1);
        }

        for (int i = 0; i < s.length(); i++) {

            char c = s.charAt(i);
            Integer size = map.get(c);
            if (size == 1) {
                return i;
            }
        }
        return -1;
    }

}
